package com.example.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.example.domain.AnnouncementDetails;
import com.example.mapper.AnnouncementsMapper;
import com.example.domain.Announcements;
import com.example.service.IAnnouncementsService;

/**
 * 通告管理Service业务层处理
 * 
 * @author 何曼伶
 * @date 2024-12-11
 */
@Service
public class AnnouncementsServiceImpl implements IAnnouncementsService 
{
    @Autowired
    private AnnouncementsMapper announcementsMapper;

    /**
     * 查询通告管理
     * 
     * @param id 通告管理主键
     * @return 通告管理
     */
    @Override
    public Announcements selectAnnouncementsById(Long id)
    {
        return announcementsMapper.selectAnnouncementsById(id);
    }

    /**
     * 查询通告管理列表
     * 
     * @param announcements 通告管理
     * @return 通告管理
     */
    @Override
    public List<Announcements> selectAnnouncementsList(Announcements announcements)
    {
        return announcementsMapper.selectAnnouncementsList(announcements);
    }

    /**
     * 新增通告管理
     * 
     * @param announcements 通告管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAnnouncements(Announcements announcements)
    {
        int rows = announcementsMapper.insertAnnouncements(announcements);
        insertAnnouncementDetails(announcements);
        return rows;
    }

    /**
     * 修改通告管理
     * 
     * @param announcements 通告管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAnnouncements(Announcements announcements)
    {
        announcementsMapper.deleteAnnouncementDetailsByAnnouncementId(announcements.getId());
        insertAnnouncementDetails(announcements);
        return announcementsMapper.updateAnnouncements(announcements);
    }

    /**
     * 批量删除通告管理
     * 
     * @param ids 需要删除的通告管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAnnouncementsByIds(Long[] ids)
    {
        announcementsMapper.deleteAnnouncementDetailsByAnnouncementIds(ids);
        return announcementsMapper.deleteAnnouncementsByIds(ids);
    }

    /**
     * 删除通告管理信息
     * 
     * @param id 通告管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAnnouncementsById(Long id)
    {
        announcementsMapper.deleteAnnouncementDetailsByAnnouncementId(id);
        return announcementsMapper.deleteAnnouncementsById(id);
    }

    /**
     * 新增通告详细信息信息
     * 
     * @param announcements 通告管理对象
     */
    public void insertAnnouncementDetails(Announcements announcements)
    {
        List<AnnouncementDetails> announcementDetailsList = announcements.getAnnouncementDetailsList();
        Long id = announcements.getId();
        if (StringUtils.isNotNull(announcementDetailsList))
        {
            List<AnnouncementDetails> list = new ArrayList<AnnouncementDetails>();
            for (AnnouncementDetails announcementDetails : announcementDetailsList)
            {
                announcementDetails.setAnnouncementId(id);
                list.add(announcementDetails);
            }
            if (list.size() > 0)
            {
                announcementsMapper.batchAnnouncementDetails(list);
            }
        }
    }
}
